from datetime import datetime
from sqlalchemy.orm import class_mapper
# from enum import unique
from App.ext import db
# from flask import Flask, render_template
# from flask_sqlalchemy import SQLAlchemy
# import pymysql
#
# pymysql.install_as_MySQLdb()
# app = Flask(__name__)
# class Config(object):
#     DEBUG = True
#     SECRET_KEY = "*)asdfasd&fsadffasdf(^(#$#8423!#"
#     # SQLALCHEMY_DATABASE_URI = "mysql://root:cjl2012@127.0.0.1:3306/axfdb"
#     SQLALCHEMY_DATABASE_URI = 'sqlite:///db/datas.db'
#     SQLALCHEMY_TRACK_MODIFICATIONS = True  # 自动跟踪数据库数据修改
#     SQLALCHEMY_ECHO = True
#
#
# app.config.from_object(Config)
# db = SQLAlchemy(app)

class BaseModel(db.Model):
    __abstract__ = True
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)

    # 封装一个数据保存
    def save(self):
        try:
            db.session.add(self)
            db.session.commit()
            return True
        except Exception as e:
            print(e)
            return False

    def delete(self):
        try:
            db.session.delete(self)
            db.session.commit()
            return True
        except Exception as e:
            print(e)
            return False

    def to_dict(self, ignore_fileds=()):  # 可以忽略不提供某些字段， 元祖格式提供
        """ 对象转字典 供 return jsonify()使用 """
        columns = [c.key for c in class_mapper(self.__class__).columns if c.key not in ignore_fileds]
        return dict((c, getattr(self, c)) for c in columns)


class Main(BaseModel):
    __abstract__ = True
    img = db.Column(db.String(200))  # 图片
    name = db.Column(db.String(100))  # 名称
    trackid = db.Column(db.String(16))  # id


class MainWheel(Main):
    __tablename__ = 'axf_wheel'


class MainNav(Main):
    __tablename__ = 'axf_nav'


class MainHotGoods(Main):
    __tablename__ = 'axf_mustbuy'  # axf_hotgoods


class MainShop(Main):
    __tablename__ = 'axf_shop'


class MainShow(Main):
    __tablename__ = 'axf_mainshow'

    categoryid = db.Column(db.String(16), nullable=False)
    brandname = db.Column(db.String(100), nullable=False)
    img1 = db.Column(db.String(200))
    childcid1 = db.Column(db.String(16), nullable=False)
    productid1 = db.Column(db.String(16), nullable=False)
    longname1 = db.Column(db.String(100))
    price1 = db.Column(db.Float(), default=0)
    marketprice1 = db.Column(db.Float(), default=0)
    img2 = db.Column(db.String(200))
    childcid2 = db.Column(db.String(16), nullable=False)
    productid2 = db.Column(db.String(16), nullable=False)
    longname2 = db.Column(db.String(100))
    price2 = db.Column(db.Float(), default=0)
    marketprice2 = db.Column(db.Float(), default=0)
    img3 = db.Column(db.String(200))
    childcid3 = db.Column(db.String(16), nullable=False)
    productid3 = db.Column(db.String(16), nullable=False)
    longname3 = db.Column(db.String(100))
    price3 = db.Column(db.Float(), default=0)
    marketprice3 = db.Column(db.Float(), default=0)


# 闪购左侧模型
class FoodType(BaseModel):
    __tablename__ = 'axf_foodtypes'

    typeid = db.Column(db.String(60))
    typename = db.Column(db.String(100))
    childtypenames = db.Column(db.String(200))
    typesort = db.Column(db.Integer, default=1)


class Goods(BaseModel):
    __tablename__ = 'axf_goods'
    productid = db.Column(db.String(16))  # 商品的id
    productimg = db.Column(db.String(200))  # 商品的图片
    productname = db.Column(db.String(100))  # 商品的名称
    productlongname = db.Column(db.String(200))  # 商品的规格
    isxf = db.Column(db.Integer, default=1)
    pmdesc = db.Column(db.String(100))
    specifics = db.Column(db.String(100))  # 规格
    price = db.Column(db.Float(), default=0)  # 商品的折后价格
    marketprice = db.Column(db.Float(), default=1)  # 商品的原价
    categoryid = db.Column(db.String(16))  # 分类的id
    childcid = db.Column(db.String(16))  # 子分类的id
    childcidname = db.Column(db.String(100))  # 子分类的名称
    dealerid = db.Column(db.String(16))
    storenums = db.Column(db.Integer, default=1)  # 排序
    productnum = db.Column(db.Integer, default=1)  # 销量排序


class UserModel(BaseModel):
    __tablename__ = 'axf_user'
    username = db.Column(db.String(32), unique=True)
    password = db.Column(db.String(256))
    email = db.Column(db.String(64), unique=True)
    # Falsh代表女
    sex = db.Column(db.Boolean, default=False)
    icon = db.Column(db.LargeBinary)  # , upload_to='icon'
    is_delete = db.Column(db.Boolean, default=False)
    ticket = db.Column(db.String(30))


# 购物车
class CarModel(BaseModel):
    __tablename__ = 'axf_cart'
    user_id = db.Column(db.Integer, db.ForeignKey(UserModel.id))
    user = db.relationship('UserModel', backref=db.backref('axf_cart', lazy='dynamic'))  # 关联用户
    goods_id = db.Column(db.Integer, db.ForeignKey(Goods.id))
    goods = db.relationship('Goods', backref=db.backref('axf_cart', lazy='dynamic'))  # 关联商品
    c_num = db.Column(db.Integer, default=1)  # 商品的个数
    is_select = db.Column(db.Boolean, default=True)  # 是否选择商品


class OrderModel(BaseModel):
    __tablename__ = 'axf_order'
    user_id = db.Column(db.Integer, db.ForeignKey(UserModel.id))
    user = db.relationship('UserModel', backref=db.backref('axf_order', lazy='dynamic'))  # 关联用户
    o_num = db.Column(db.String(64))  # 总价 收货人信息地址 订单备注信息字段 等
    # 0 代表下单，但是未付款 1 代表已付款未发货 2 已付款。已发货
    o_status = db.Column(db.Integer, default=0)  # 状态
    o_create = db.Column(db.DateTime, default=datetime.utcnow)  # 创建时间


class OrderGoodsModel(BaseModel):
    __tablename__ = 'axf_order_goods'
    goods_id = db.Column(db.Integer, db.ForeignKey(Goods.id))
    goods = db.relationship('Goods', backref=db.backref('axf_order_goods', lazy='dynamic'))  # 关联商品
    order_id = db.Column(db.Integer, db.ForeignKey(OrderModel.id))
    order = db.relationship('OrderModel', backref=db.backref('axf_order_goods', lazy='dynamic'))  # 关联的订单
    good_num = db.Column(db.Integer, default=1)  # 商品的个数


class UserTicketModel(BaseModel):
    __tablename__ = 'axf_user_ticket'
    user_id = db.Column(db.Integer, db.ForeignKey(UserModel.id))
    user = db.relationship('UserModel', backref=db.backref('axf_user_ticket', lazy='dynamic'))
    ticket = db.Column(db.String(30), nullable=False)
    out_time = db.Column(db.DateTime)
